﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pong.Classes;
using System;

namespace Pong
{
    public class Bat : Sprite
    {
        public int WindowHeight { get; set; }
        public PowerClass MyClass { get; set; }
        public int WindowWidth { get; set; }
        public int Score { get; set; }
        public float Scale { get; set; }

        public int vectorZP1 { get; set; }
        public int vectorSP1 { get; set; }
        public int vectorUPP2 { get; set; }
        public int vectorDOWNP2 { get; set; }
       

        // Collision box creation
        public Rectangle CollisionBox
        {
            get
            {
                return new Rectangle((int)Position.X, (int)Position.Y, (int)(Texture.Width * Scale), (int)(Texture.Height * Scale));
            }
        }

        private int _playerNumber;

        public Bat(int windowWidth, int windowHeight, float scale, int playerNumber, PowerClass pC, Boolean otherPlayerIsManipulator)
        {
            WindowWidth = windowWidth;
            WindowHeight = windowHeight;
            Scale = scale;
            this.MyClass = pC;
            _playerNumber = playerNumber;

            this.vectorUPP2 = 1;
            this.vectorDOWNP2 = -1;
            this.vectorZP1 = -1;
            this.vectorSP1 = 1;

            if (otherPlayerIsManipulator && playerNumber == 1)
            {
                this.vectorZP1 = 1;
                this.vectorSP1 = -1;
            }
            else if (!otherPlayerIsManipulator && playerNumber == 1) 
            {
                this.vectorZP1 = -1;
                this.vectorSP1 = 1;
            }

            if (otherPlayerIsManipulator && playerNumber == 2)
            {
                this.vectorUPP2 = 1;
                this.vectorDOWNP2 = -1;
            }
            else if (!otherPlayerIsManipulator && playerNumber == 2)
            {
                this.vectorUPP2 = -1;
                this.vectorDOWNP2 = 1;
            }
        }

        public override void Initialize()
        {
            base.Initialize();

            Score = 0;
        }

        public override void LoadContent(ContentManager content, string assetName)
        {
            base.LoadContent(content, assetName);

            // Depending on the player, the bat is placed on either side of the screen
            if (_playerNumber == 1)
            {
                Position = new Vector2(WindowWidth * 0.01f, WindowHeight / 2 - Texture.Height * Scale / 2);
            }
            else if (_playerNumber == 2 || _playerNumber == 3 || _playerNumber == 4)
            {
                Position = new Vector2(WindowWidth - (WindowWidth * 0.01f) - Texture.Width * Scale, WindowHeight / 2 - Texture.Height * Scale / 2);
            }
        }

        public override void HandleInput(KeyboardState keyboardState, MouseState mouseState, Ball ball)
        {
            float centerOfBat = Position.Y + ((Texture.Height * Scale) / 2);
            float centerOfBall = ball.Position.Y + ((ball.Texture.Height * Scale) / 2);



            if (_playerNumber == 1)
            {
               
                if (keyboardState.IsKeyDown(Keys.Z))
                {
                    Direction = vectorZP1*(Vector2.UnitY);
                    Speed = WindowWidth * MyClass.SpeedBat;
                }
                else if (keyboardState.IsKeyDown(Keys.S))
                {
                    Direction = vectorSP1 * (Vector2.UnitY);
                    Speed = WindowWidth * MyClass.SpeedBat;
                }
                else
                {
                    Speed = 0;
                }
            }
            else if (_playerNumber == 2)
            {
               
                if (keyboardState.IsKeyDown(Keys.Up))
                {
                    Direction = vectorUPP2 *( Vector2.UnitY);
                    Speed = WindowWidth * MyClass.SpeedBat;
                }
                else if (keyboardState.IsKeyDown(Keys.Down))
                {
                    Direction = vectorDOWNP2 *( Vector2.UnitY);
                    Speed = WindowWidth * MyClass.SpeedBat;
                }
                else
                {
                    Speed = 0;
                }
            }
            else if (_playerNumber == 3) //Basic AI control
            {
                if (ball.Direction.X > 0)
                {
                    if (centerOfBall < Position.Y)
                    {
                        Direction = -Vector2.UnitY;
                        Speed = WindowWidth * MyConstants.SPEED_BAT_CLASSIC * ((centerOfBat - centerOfBall) / (Texture.Height * Scale));
                    }

                    if (centerOfBall > Position.Y + (ball.Texture.Height * Scale) / 2)
                    {
                        Direction = Vector2.UnitY;
                        Speed = WindowWidth * MyConstants.SPEED_BAT_CLASSIC * ((centerOfBall - centerOfBat) / (Texture.Height * Scale));
                    }
                }
                else
                    Speed = 0;
            }
            else if (_playerNumber == 4) //Invincible AI
            {
                if (ball.Direction.X > 0)
                {
                    if (centerOfBall < Position.Y)
                    {
                        Direction = -Vector2.UnitY;
                        Speed = WindowWidth * MyConstants.SPEED_BAT_SURVIVAL * ((centerOfBat - centerOfBall + MyConstants.SPEED_BAT_CLASSIC) / (Texture.Height * Scale));
                    }

                    if (centerOfBall > Position.Y + (ball.Texture.Height * Scale) / 2) 
                    {
                        Direction = Vector2.UnitY;
                        Speed = WindowWidth * MyConstants.SPEED_BAT_SURVIVAL * ((centerOfBall - centerOfBat + MyConstants.SPEED_BAT_CLASSIC) / (Texture.Height * Scale));
                    }
                }
                else
                    Speed = 0;
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Position.Y <= 0 && Direction.Y < 0)
                Speed = 0;
            if (Position.Y >= WindowHeight - Texture.Height * Scale && Direction.Y > 0)
                Speed = 0;

            base.Update(gameTime);
        }
    }
}